﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using log4net;
using Matrix.Common.Utility;
using Matrix.MaKaYunShang.DAL;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.BLL
{
    public class CommodityLibBLL : BaseBLL<CommodityLibBLL>
    {
        private readonly ILog _fileLog = LogManager.GetLogger(typeof(CommodityLibBLL));

        #region 默认方法

        /// <summary>
        ///     增加一条数据
        /// </summary>
        public int Add(CommodityLib model)
        {
            return CommodityLibDAL.Instance.Add(model);
        }

        /// <summary>
        ///     更新一条数据
        /// </summary>
        public bool Update(CommodityLib model)
        {
            return CommodityLibDAL.Instance.Update(model);
        }

        /// <summary>
        ///     删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            return CommodityLibDAL.Instance.Delete(Id);
        }

        /// <summary>
        ///     批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return CommodityLibDAL.Instance.DeleteList(Idlist);
        }

        /// <summary>
        ///     得到一个对象实体
        /// </summary>
        public CommodityLib GetModel(int Id)
        {
            DataSet ds = CommodityLibDAL.Instance.GetModel(Id);
            return ds == null ? null : ServicesHelper.DataSetToIList<CommodityLib>(ds).ToList()[0];
        }

        /// <summary>
        ///     获得数据列表
        /// </summary>
        public List<CommodityLib> GetModelList(string strWhere = null)
        {
            DataSet ds = CommodityLibDAL.Instance.GetList(strWhere);
            return ServicesHelper.DataSetToIList<CommodityLib>(ds).ToList();
        }

        #endregion

        #region 扩展方法

        /// <summary>
        ///     获得分页数据列表
        /// </summary>
        public TableDataViewDto<ViewCommodityLibDto> GetPagingList(int draw, int pageIndex, int pageSize,
            string searchKeyWord, string colOrder, bool isHideShowLanmaSold = false)
        {
            try
            {
                var totalCount = 0;
                var list = CommodityLibDAL.Instance.GetPagingList(pageIndex, pageSize,
                    searchKeyWord, colOrder, isHideShowLanmaSold, ref totalCount);

                var returnDataList = new TableDataViewDto<ViewCommodityLibDto>
                {
                    data = list,
                    draw = draw,
                    recordsFiltered = totalCount,
                    recordsTotal = totalCount
                };
                return returnDataList;
            }
            catch (Exception ex)
            {
                _fileLog.Error(" GetPagingList | Exception: " + ex);
                return null;
            }
        }

        /// <summary>
        ///     获取商品全信息
        /// </summary>
        /// <returns></returns>
        public CommodityDto GetCommodityFullInfo(int commodifyLibId)
        {
            DataSet ds = CommodityLibDAL.Instance.GetCommodityFullInfo(commodifyLibId);
            if (ds == null || ds.Tables.Count < 4)
            {
                return null;
            }

            var commodityDto = new CommodityDto();
            if (ds.Tables[0].Rows.Count > 0)
            {
                List<CommodityLib> lsData =
                    ServicesHelper.DataTableToListModel<CommodityLib>.ConvertToModel(ds.Tables[0]).ToList();
                if (lsData.Count > 0)
                {
                    commodityDto.BaseCommodityInfo = lsData[0];
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

            commodityDto.GiftList = new List<CommodityGift>();
            if (ds.Tables[1].Rows.Count > 0)
            {
                List<CommodityGift> lsData =
                    ServicesHelper.DataTableToListModel<CommodityGift>.ConvertToModel(ds.Tables[1]).ToList();
                if (lsData.Count > 0)
                {
                    commodityDto.GiftList = lsData.ToList();
                }
            }

            commodityDto.PictureList = new List<CommodityPicture>();
            if (ds.Tables[2].Rows.Count > 0)
            {
                List<CommodityPicture> lsData =
                    ServicesHelper.DataTableToListModel<CommodityPicture>.ConvertToModel(ds.Tables[2]).ToList();
                if (lsData.Count > 0)
                {
                    commodityDto.PictureList = lsData.ToList();
                }
            }

            commodityDto.CouponGiftList = new List<CommodityCouponGift>();
            if (ds.Tables[2].Rows.Count > 0)
            {
                List<CommodityCouponGift> lsData =
                    ServicesHelper.DataTableToListModel<CommodityCouponGift>.ConvertToModel(ds.Tables[3]).ToList();
                if (lsData.Count > 0)
                {
                    commodityDto.CouponGiftList = lsData.ToList();
                }
            }

            return commodityDto;
        }

        /// <summary>
        ///     获取指定商品ID集合的商品全信息列表
        /// </summary>
        /// <param name="commodityIds">商品ID集合</param>
        /// <returns></returns>
        public List<CommodityDto> GetCommodityFullInfoAppointedIds(string commodityIds)
        {
            string[] arrCommodityId = commodityIds.Split(',');
            foreach (string s in arrCommodityId)
            {
                int nCommodityId;
                if (!int.TryParse(s, out nCommodityId))
                {
                    _fileLog.Error("CommodityLibBLL | GetCommodityFullInfoAppointedIds | Invalid Commodity Ids - " +
                                   commodityIds);
                    return null;
                }
            }

            DataSet ds = CommodityLibDAL.Instance.GetCommodityFullInfoAppointedIds(commodityIds);
            if (ds == null || ds.Tables.Count < 4)
            {
                return null;
            }

            List<CommodityLib> lsCommodityLib;
            if (ds.Tables[0].Rows.Count > 0)
            {
                lsCommodityLib = ServicesHelper.DataTableToListModel<CommodityLib>.ConvertToModel(ds.Tables[0]).ToList();
            }
            else
            {
                return null;
            }

            var lsGiftList = new List<CommodityGift>();
            if (ds.Tables[1].Rows.Count > 0)
            {
                lsGiftList = ServicesHelper.DataTableToListModel<CommodityGift>.ConvertToModel(ds.Tables[1]).ToList();
            }

            var lsPictureList = new List<CommodityPicture>();
            if (ds.Tables[2].Rows.Count > 0)
            {
                lsPictureList =
                    ServicesHelper.DataTableToListModel<CommodityPicture>.ConvertToModel(ds.Tables[2]).ToList();
            }

            var lsCouponGiftList = new List<CommodityCouponGift>();
            if (ds.Tables[3].Rows.Count > 0)
            {
                lsCouponGiftList =
                    ServicesHelper.DataTableToListModel<CommodityCouponGift>.ConvertToModel(ds.Tables[3]).ToList();
            }

            return (from commodity in lsCommodityLib
                    let lsCommodityGift = lsGiftList.FindAll(p => p.BelongToCommodityLibId == commodity.Id)
                    let lsCommodityPicture = lsPictureList.FindAll(p => p.CommodityLibId == commodity.Id)
                    let lsCommodityCouponGift = lsCouponGiftList.FindAll(p => p.BelongToCommodityLibId == commodity.Id)
                    select new CommodityDto
                    {
                        BaseCommodityInfo = commodity,
                        GiftList = lsCommodityGift,
                        PictureList = lsCommodityPicture,
                        CouponGiftList = lsCommodityCouponGift
                    }).ToList();
        }

        /// <summary>
        ///     通过店铺商品ID获取商品基本信息列表
        /// </summary>
        /// <param name="shopCommodityIds"></param>
        /// <returns></returns>
        public List<CommodityLib> GetCommodityInfoByAppointedShopCommodityIds(string shopCommodityIds)
        {
            string[] arrCommodityId = shopCommodityIds.Split(',');
            foreach (string s in arrCommodityId)
            {
                int nCommodityId;
                if (!int.TryParse(s, out nCommodityId))
                {
                    _fileLog.Error(
                        "CommodityLibBLL | GetCommodityInfoByAppointedShopCommodityIds | Invalid ShopCommodity Ids - " +
                        shopCommodityIds);
                    return null;
                }
            }

            return CommodityLibDAL.Instance.GetCommodityInfoByAppointedShopCommodityIds(shopCommodityIds);
        }

        /// <summary>
        /// 根据条件获取库商品总数
        /// </summary>
        /// <remarks>2016-08-22 万乐园 创建</remarks>
        public int GetTotalCount(string strWhere = null)
        {
            return CommodityLibDAL.Instance.GetTotalCount(strWhere);
        }

        public List<CommodityLib> GetListByCommodityName(string commodityName)
        {
            DataSet ds = CommodityLibDAL.Instance.GetListByCommodityName(commodityName);
            return ServicesHelper.DataSetToIList<CommodityLib>(ds).ToList();
        }

        /// <summary>
        /// 根据shopCommodityIds获得数据列表
        /// </summary>
        public List<CommodityLib> GetListByShopCommodityIds(string shopCommodityIds)
        {
            DataSet ds = CommodityLibDAL.Instance.GetListByShopCommodityIds(shopCommodityIds);
            return ServicesHelper.DataSetToIList<CommodityLib>(ds).ToList();
        }

        #endregion
    }
}